package com.woniu.punishcomplaintnotice.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniu.common.entity.PageResult;
import com.woniu.common.entity.PunishmentRecord;
import com.woniu.punishcomplaintnotice.mapper.PunishMapper;
import com.woniu.punishcomplaintnotice.service.PunishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PunishServiceImpl implements PunishService {
    @Autowired
    PunishMapper punishMapper;

    // 封装分页信息
    public <T> PageResult<T> getResult(int page, int size, Page<T> pageHelper, List<T> t) {
        // 获取分页信息
        PageResult<T> pageResult = new PageResult<>();
        // 当前页
        pageResult.setCurrentPage(page);
        // 每一页大小
        pageResult.setSize(size);
        // 总条数
        pageResult.setTotal(pageHelper.getTotal());
        // 总页数
        pageResult.setTotalPage(pageHelper.getPages());
        // 查询的数据
        pageResult.setDatas(t);

        return pageResult;
    }

    @Override
    public void insertPunish(PunishmentRecord punishmentRecord) {
        punishMapper.insertPunishs(punishmentRecord);
    }

    @Override
    public PageResult<PunishmentRecord> findAllPunishment(Integer page,Integer size) {

        List<PunishmentRecord> allRecord = punishMapper.findAllPunishment();
        Page<PunishmentRecord> startPage = PageHelper.startPage(page, size);

        return getResult(page, size, startPage, allRecord);
    }

    //通过u_id 查询惩罚记录
    public PageResult<PunishmentRecord> findPunishmentByUid(Integer u_id, Integer page, Integer size) {

        Page<PunishmentRecord> pageHelper = PageHelper.startPage(page, size);
        List<PunishmentRecord> punishmentRecords = punishMapper.findPunishmentByu_id(u_id);

        return getResult(page, size, pageHelper, punishmentRecords);
    }
}
